#测试文件目录页的元素
import re
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from common.Utils import CloudDriver

class Category:
    url=""
    driver=""
    def __init__(self):
        self.url="http://vnollx.cloud/homepage"
        self.driver=CloudDriver.driver
        self.driver.get(self.url)


    #测试文件目录页，是在iframe中，也需要切换上下文
    def FileDirectory(self):
        #由于这几个页面按钮都在"http://vnollx.cloud/homepage"页面下，需要点击对应栏目找到对应的iframe
        self.driver.find_element(By.CSS_SELECTOR,"#allfilelink").click()
        #切换
        iframe = self.driver.find_element(By.TAG_NAME, "iframe")
        self.driver.switch_to.frame(iframe)
        #点击上传文件按钮
        self.driver.find_element(By.CSS_SELECTOR,"body > div.file-container > div.create-folder-btn > button:nth-child(1)").click()

        #1、不填写信息直接点击上传
        self.driver.find_element(By.CSS_SELECTOR,"#uploadModal > div > div.modal-actions > button.confirm-btn").click()
        #查看弹窗信息
        word=self.driver.find_element(By.CSS_SELECTOR,"#swal2-title").text
        assert word=="请选择要上传的文件"
        #点击ok
        self.driver.find_element(By.CSS_SELECTOR,"body > div.swal2-container.swal2-center.swal2-backdrop-show > div > div.swal2-actions > button.swal2-confirm.swal2-styled").click()

        #2、测试填写信息后上传
        #选择文件
        self.driver.find_element(By.CSS_SELECTOR,"#uploadFile").send_keys("E:\\Wallpapers\\img.png")
        #输入文件名
        self.driver.find_element(By.CSS_SELECTOR,"#uploadFileName").send_keys("picture")
        #点击上传/或取消，此处测试上传
        self.driver.find_element(By.CSS_SELECTOR,"#uploadModal > div > div.modal-actions > button.confirm-btn").click()
        #3、测试创建文件夹body > div.file-container > div.create-folder-btn > button:nth-child(2)
        time.sleep(5)
        self.driver.find_element(By.CSS_SELECTOR,"body > div.file-container > div.create-folder-btn > button:nth-child(2)").click()

        #不填写信息直接创建，让其弹出警告弹窗
        self.driver.find_element(By.CSS_SELECTOR,"#createFolderModal > div > div.modal-actions > button.confirm-btn").click()
        #提示弹窗
        alert = self.driver.switch_to.alert
        alert.accept()  # 点击确定
        #输入名字
        self.driver.find_element(By.CSS_SELECTOR,"#folderName").send_keys("file")
        #再次点击
        self.driver.find_element(By.CSS_SELECTOR,"#createFolderModal > div > div.modal-actions > button.confirm-btn").click()
        # 通过遍历目录栏中的所有文件名，判断是否创建成功
        # 需要刷新,该项目在刷新后又回到了第一个页面所有得先切换
        # self.driver.switch_to.default_content()
        self.driver.refresh()
        # 再回到该页面
        self.driver.find_element(By.CSS_SELECTOR, "#allfilelink").click()
        # 再切换到iframe
        iframe = self.driver.find_element(By.TAG_NAME, "iframe")
        self.driver.switch_to.frame(iframe)
        # 获取所有文件夹名
        # 1. 先定位表格（推荐使用更稳定的定位方式）
        table = self.driver.find_element(By.XPATH, "/html/body/div[2]/table")
        # 2. 然后定位所有行
        file_elements = table.find_elements(By.XPATH, "./tbody/tr")
        filenames = [elem.text for elem in file_elements]
        pure_filenames = []
        for row in filenames:
            # 获取完整信息（或者更好的方式是直接定位到只包含文件名的元素）
            # full_info = row.text
            # 使用正则表达式提取纯文件名
            match = re.search(r'[️📁]\s*([^ 文件文件夹]+)', row)
            if match:
                pure_name = match.group(1).strip()
                pure_filenames.append(pure_name)

        # 检查文件夹名是否在列表中
        filename="file"
        for name in pure_filenames:
            if name==filename:
                print(f"文件夹 '{filename}' 存在于列表中，创建成功")